#pragma once

enum Color
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode
{
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;

	pair<K, V> _kv;
	Color _col;

	RBTreeNode(const pair<K, V>& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_kv(kv)
	{}
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		cur->_col = RED;

		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			assert(grandfather);
			assert(grandfather->_col == BLACK);

			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
				}
			}
		}

		_root->_col = BLACK;

		return true;
	}

	~RBTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

	bool is_RBTree_True()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
		{
			cout << "根节点不是黑色~" << endl;
			return false;
		}

		int benchmark = 0;

		return Check(_root, 0, benchmark);
	}

	void InOrder() { _InOrder(_root); }

private:
	void _InOrder(Node* root)
	{
		if (root == nullptr) return;

		_InOrder(root->_left);
		cout << root->_kv.first << ':' << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	bool Check(Node* root, int blacknum, int& benchmark)
	{
		if (root == nullptr)
		{
			if (benchmark == 0)
				benchmark = blacknum;
			if (benchmark != blacknum)
				return false;
			return true;
		}

		if (root->_col == BLACK)
			blacknum++;

		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "存在连续的红色节点~" << endl;
			return false;
		}

		return Check(root->_left, blacknum, benchmark)
			&& Check(root->_right, blacknum, benchmark);
	}

	void Destroy(Node* root)
	{
		if (root == nullptr) return;
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}

	void RotateL(Node* parent)
	{
		Node* sub = parent->_right;
		Node* subL = sub->_left;

		parent->_right = subL;
		if (subL)
			subL->_parent = parent;

		Node* ppNode = parent->_parent;
		sub->_left = parent;
		parent->_parent = sub;

		if (parent == _root)
		{
			_root = sub;
			sub->_parent = nullptr;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = sub;
			}
			else
			{
				ppNode->_right = sub;
			}
			sub->_parent = ppNode;
		}
	}

	void RotateR(Node* parent)
	{
		Node* sub = parent->_left;
		Node* subR = sub->_right;

		parent->_left = subR;
		if (subR)
			subR->_parent = parent;

		Node* ppNode = parent->_parent;
		sub->_right = parent;
		parent->_parent = sub;

		if (parent == _root)
		{
			_root = sub;
			sub->_parent = nullptr;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = sub;
			}
			else
			{
				ppNode->_right = sub;
			}
			sub->_parent = ppNode;
		}
	}

private:
	Node* _root = nullptr;
};

